package com.toannv.handler;

import java.util.List;

import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.variables.ReservedRoomVariables;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;
import com.smartfoxserver.v2.game.SFSGame;

public class leaveRoomHandler extends BaseServerEventHandler{

	@Override
	public void handleServerEvent(ISFSEvent event) throws SFSException {
		// TODO Auto-generated method stub
		SFSGame CurrentRoom = (SFSGame) event.getParameter(SFSEventParam.ROOM);
		User sender = (User) event.getParameter(SFSEventParam.USER);
		//neu phong choi co ton tai nguoi yeu cau thoat khoi phong
		if(CurrentRoom.containsUser(sender)){
			
			if(!CurrentRoom.getVariable(ReservedRoomVariables.RV_GAME_STARTED).isNull()){
				//get danh sach nhung nguoi dang la khach trong phong
				List<User> spectators = CurrentRoom.getSpectatorsList();
				//xoa bo nguoi choi sender ra phoi danh sach nguoi choi
				CurrentRoom.removeUser(sender);
				
				if(spectators.size()>0){
					//neu danh sach khach lon hon 0 thi cho phep khach co index 0 lam nguoi choi
					CurrentRoom.addUser(spectators.get(0));
				}
			}
			
			else{
				
			}
		}
		//neu phong choi khong ton tai nguoi yeu cau thoat khoi phong
		else{
			//SFSErrorData err = new SFSErrorData(SFSErrorCode.)
			
		}
	}

}
